package com.bootdo.haojiangboutils;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.newmenu.MenuMeta;
import com.bootdo.newmenu.MenuModel;
import com.bootdo.system.domain.MenuDO;

import java.util.ArrayList;
import java.util.List;

public class MenuVueUtils {
    public static void copyNewMenuObject(List<MenuModel> list, List<Tree<MenuDO>> treeList){
        treeList.forEach(item ->{
            MenuModel menuModel = new MenuModel();
            copyMenuObject(list,menuModel,item);
        });
    }

    private static void copyMenuObject(List<MenuModel> list,MenuModel menuModel,Tree<MenuDO> tree ){
        if(null != tree && tree.getAttributes().get("url")  != null && StringUtils.isNotEmpty(tree.getAttributes().get("url").toString())){
            menuModel.setName(tree.getAttributes().get("url").toString());
            menuModel.setComponent((null == tree.getAttributes().get("path") || StringUtils.isEmpty(tree.getAttributes().get("path").toString())) ? "":tree.getAttributes().get("path").toString());
            MenuMeta menuMeta = new MenuMeta(tree.getText(),
                    tree.getAttributes().get("icon") == null ? "" :  tree.getAttributes().get("icon").toString() , (Integer) tree.getAttributes().get("isShow"));
            menuModel.setMeta(menuMeta);
            menuModel.setPath(tree.getAttributes().get("url").toString());
            menuModel.setRedirect("");
            list.add(menuModel);
            List<MenuModel> clist = new ArrayList<>();
            menuModel.setChildren(clist);
            if(tree.getChildren() != null && tree.getChildren().size() > 0){
                copyNewMenuObject(clist,tree.getChildren());
            }
        }
    }
}
